#include <cstdlib>
#include <regex>
#include <locale>
#include <climits>
#include <cmath>
#include "Tools.h"

using namespace raph::tools;
using namespace raph::type;

namespace raph
{
/*-------------------------------------------------------------------
 CONVERT
 -------------------------------------------------------------------*/
T_UINT16 Convert::charToUint16(const char& elem)
    {
    char c[2];
    c[0] = elem;
    c[1] = '\0';
    return (T_UINT16) atoi(c);
    }

std::list<T_UINT16>* Convert::stringNumberToList(const std::string& str)
    {
    std::list<T_UINT16> *list = new std::list<T_UINT16>();

//	std::regex integer("(\\+|-)?[[:digit:]]+");
//	if (!std::regex_match(str, integer))
//		{
//		std::cout<<"nan"<<std::endl;
//		list->push_back(new uint16(0));
//		return list;
//		}

    for (const char &elem : str)
        {
        if (isdigit(elem))
            {
            list->push_back(charToUint16(elem));
            }
        else
            {
            list->clear();
            list->push_back(0);
            break;
            }
        }
    return list;
    }

/*-------------------------------------------------------------------
 NUMBER
 -------------------------------------------------------------------*/
bool Numbers::isEqual(const type::T_FLOAT128& a, const type::T_FLOAT128& b,
        const type::T_FLOAT128& percentage) noexcept
    {
    return std::fabs(a - b) <= a * percentage / 100;
    }

bool Numbers::isPrime(const unsigned int &n)
    {
    if (n == 0 || n == 1)
        {
        return false;
        }

    int temp = n / 2;
    int i = temp;
    while (i > 1)
        {
        if (n % i == 0)
            {
            return false;
            }
        --i;
        }

    return true;
    }
}

